<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class props extends CI_Controller{
	public function __construct(){
		parent::__construct();
		if(!isset($_SESSION["isadmin"]) || !$_SESSION["isadmin"]){
			header("location:/index.php/login");
		}
	}
	
	public function propsList(){
            $url = "/index.php?c=props&a=propsList";
            $title = isset( $_GET['title']) ? $_GET['title'] : '';
            $data['title'] = $title;
            $where = "";
            if( $title ){
                $where  = " WHERE title like '%{$title}%' " ;
                $url.= "&title=".$title;
            }
            $this->config->load('pagination',true);
            $pagesize  = $this->config->item("per_page","pagination");
            $sposition  = isset($_GET['per_page']) && (int)$_GET['per_page'] ? (int)$_GET['per_page'] : 0;
            
            $sql = " SELECT  * FROM pk_props $where ORDER BY ctime DESC  LIMIT $sposition ,$pagesize ";
            $this->load->model('PropsModel');
            $data['propsList'] = $this->PropsModel->getAll($sql);
            
            $this->load->library('pagination');
            $config['base_url'] = $url;
            $config['total_rows'] = $this->PropsModel->propsCount($where);
            $this->pagination->initialize($config);
            $data['page'] = $this->pagination->create_links();
             
            $this->load->view("header");
            $this->load->view("props/list",$data);
            $this->load->view("footer");
	}
	
	public function propsAdd(){
		$this->load->view("header");
		$this->load->view("props/add");
		$this->load->view("footer");
	}
        
        public function propsEdit(){
                $propsID = isset($_GET['propsID']) && 0 < intval($_GET['propsID']) ? intval($_GET['propsID']) : '';
                if( !$propsID ) mcmessage_error('出错了,请重试！', '/index.php?c=props&a=propsList');
                $data['propsID'] = $propsID;
                $this->load->model('PropsModel');
                $sql = " SELECT * FROM pk_props WHERE id ='{$propsID}' LIMIT 1 ";
                
                $data['props'] = $this->PropsModel->getRow($sql);
                $this->load->view("header");
		$this->load->view("props/add" ,$data );
		$this->load->view("footer");
        }
        
        public function commodityList(){
            $url = "/index.php?c=props&a=commodityList";
            $title = isset( $_GET['title']) ? $_GET['title'] : '';
            $data['title'] = $title;
            $where = "";
            if( $title ){
                $where  = " WHERE title like '%{$title}%' " ;
                $url.= "&title=".$title;
            }
            $this->config->load('pagination',true);
            $pagesize  = $this->config->item("per_page","pagination");
            $sposition  = isset($_GET['per_page']) && (int)$_GET['per_page'] ? (int)$_GET['per_page'] : 0;
            
            $sql = " SELECT  * FROM pk_commodity $where ORDER BY ctime DESC  LIMIT $sposition ,$pagesize ";
            $this->load->model('PropsModel');
            $data['commodityList'] = $this->PropsModel->getAll($sql);
            
            $this->load->library('pagination');
            $config['base_url'] = $url;
            $config['total_rows'] = $this->PropsModel->commodityCount($where);
            $this->pagination->initialize($config);
            $data['page'] = $this->pagination->create_links();
             
            $this->load->view("header");
            $this->load->view("props/commodityList",$data);
            $this->load->view("footer");

        }
        public function commodityAdd(){
            
            $this->load->model('PropsModel');
            $sql2 = "SELECT * FROM pk_props WHERE isDel = 0 ";
            $data['propsList'] = $this->PropsModel->getAll($sql2 );
                    
            $this->load->view("header");
            $this->load->view("props/commodityAdd" ,$data);
            $this->load->view("footer");     
        }
        public function commodityEdit(){
            $commodityID = isset($_GET['commodityID']) && 0 < intval($_GET['commodityID']) ? intval($_GET['commodityID']) : '';
            if( !$commodityID ) mcmessage_error('出错了,请重试！', '/index.php?c=props&a=commodityList');
            $data['commodityID'] = $commodityID;
            $this->load->model('PropsModel');
            $sql = " SELECT * FROM pk_commodity WHERE id ='{$commodityID}' LIMIT 1 ";
                
            $data['commodity'] = $this->PropsModel->getRow($sql);
            $sql2 = "SELECT * FROM pk_props WHERE isDel = 0 ";
            $data['propsList'] = $this->PropsModel->getAll($sql2 );
            
            $this->load->view("header");
            $this->load->view("props/commodityAdd",$data);
            $this->load->view("footer");     
        }
        
        public function commoditySave(){
            $info = $_POST;
            $set  = array();
            if(!empty($info['title'])){
                $set[] = "`title`='{$info['title']}'";
            }
            if(0<intval($info['price'])){
                $set[] = "`price`='{$info['price']}'";
            }
            if(!empty($info['pType'])){
                $set[] = "`pType`='{$info['pType']}'";
            }
            if(!empty($info['discount'])){
                $set[] = "`discount`='{$info['discount']}'";
            }
            if(!empty($info['stick'])){
                $set[] = "`stick`='{$info['stick']}'";
            }
            if(!empty($info['state'])){
                $set[] = "`state`='{$info['state']}'";
            }
            if(!empty($info['descripe'])){
                $set[] = "`descripe`='{$info['descripe']}'";
            }
            if(!empty($info['rander'])){
                $set[] = "`rander`='{$info['rander']}'";
            }
            $pros = array();
            foreach($info['propsIDS']as $k=>$v ){
                $fo = explode('-', $v);
                $pros[$k]['id'] = $fo[0];
                $pros[$k]['title'] = $fo[1];
                $pros[$k]['propsNum'] = $info['propsNum'][$k];
            }
            $set[] = " `propsList`='".serialize($pros)."'";
            
            if( isset($info['commodityID']) && 0 < intval($info['commodityID']) ){
                    $sql = "UPDATE pk_commodity SET ".implode(",", $set)." WHERE id='{$info['commodityID']}'";
                    $message = "修改商品";
                }else{
                    $set[] = "`ctime`='".time()."'";   
                    $sql = "INSERT INTO pk_commodity SET ".implode(",", $set);
                    $message = "新增商品";
                }
                $this->load->model('PropsModel');
                if( $this->PropsModel->updateState($sql)){
                    mcmessage_error($message.'成功！', '/index.php?c=props&a=commodityList');
                }else{
                    mcmessage_error($message.'失败！', '/index.php?c=props&a=commodityList');
                }
        }
        
        public function commodityDel(){
            
            $commodityID = isset($_GET['commodityID']) && 0 < intval($_GET['commodityID']) ? $_GET['commodityID'] : '';
            if(!$commodityID){
                mcmessage_error("出错了，请重试！",  '/index.php?c=props&a=commodityList' );
            }
            $sql = " UPDATE pk_commodity SET isDel =1  WHERE id ='{$commodityID}'";
            $this->load->model('PropsModel');
                if( $this->PropsModel->updateState($sql)){
                    mcmessage_error('删除成功！', '/index.php?c=props&a=commodityList');
                }else{
                    mcmessage_error('删除失败！', '/index.php?c=props&a=commodityList');
                }
        }
        
        public function propsSave(){
                $info = $_POST;
                $set = array();
                if(isset($info['title'])){
                    $set[] = "`title` = '{$info['title']}'";
                }
                if(isset($info['type'])){
                    $set[] = "`type` = '{$info['type']}'";
                    $useJson = $this->getUseJson( $info );
                    $set[] = "`useJson` = '".$useJson."'";
                }
                if(isset($info['descripe'])){
                    $set[] = "`describe` = '{$info['descripe']}'";
                }
                /*if(isset($_FILES['img']) && !empty($_FILES['img']) ){
                    $img = $this->saveImg($_FILES['img']);
                    if($img){
                        $set[] = "`img` = '{$img}'";
                    }
                }*/
                if(isset($info['img']) && !empty($info['img']) ){
                    $set[] = "`img` = '{$info['img']}'";
                }
                $set[] = "`rander` = '{$info['rander']}'";
                
                if( isset($info['propsID']) && 0 < intval($info['propsID']) ){
                    $sql = "UPDATE pk_props SET ".implode(",", $set)." WHERE id='{$info['propsID']}'";
                    $message = "修改道具";
                }else{
                    $set[] = "`ctime`='".time()."'";   
                    $sql = "INSERT INTO pk_props SET ".implode(",", $set);
                    $message = "新增道具";
                }
                $this->load->model('PropsModel');
                if( $this->PropsModel->updateState($sql)){
                    mcmessage_error($message.'成功！', '/index.php?c=props&a=propsList');
                }else{
                    mcmessage_error($message.'失败！', '/index.php?c=props&a=propsList');
                }
        }
        
        public function propsDel(){
            $propsID = isset($_GET['propsID']) && 0 < intval($_GET['propsID']) ? $_GET['propsID'] : '';
            if(!$propsID){
                mcmessage_error("出错了，请重试！",  '/index.php?c=props&a=propsList' );
            }
            $sql = " UPDATE pk_props SET isDel =1  WHERE id ='{$propsID}'";
            $this->load->model('PropsModel');
                if( $this->PropsModel->updateState($sql)){
                    mcmessage_error('删除成功！', '/index.php?c=props&a=propsList');
                }else{
                    mcmessage_error('删除失败！', '/index.php?c=props&a=propsList');
                }
         }
        
        public function saveImg($info){
            $path = CIROOT."upload/".date('Ymd');
            if(!file_exists($path)){
                mkdir($path);
            }  
            $arr = explode('.', $info['name']);
            $type = $arr[count($arr)-1];
            $name = time().rand(10000,99999);
            $newName = $name.".".$type;
            $file2 = $path.'/'.$newName;
            if( move_uploaded_file( $info['tmp_name'], $file2) ){
                return date('Ymd').'/'.$newName;
            }else{
                return '';
            }

        }
        
        public function getUseJson($info){
            $str = '';
            switch($info['type']){
                case 1:
                    $data['days'] = $info['days'];
                    $data['jetton'] = $info['jetton'];
                    $str = serialize($data);
                    break;
                case 2:
                    $data['browVal'] = $info['browVal'];
                    $str = serialize($data);
                    break;
                case 3:
                    $data['trumpetVal'] = $info['trumpetVal'];
                    $str = serialize($data);
                    break;
                case 4:
                   break;
                case 5:
                    $data['makeLeaveVal'] = $info['makeLeaveVal']*60;
                    $str = serialize($data);
                    break;
                case 6:
                    $data['lossOdds'] = $info['lossOdds'];
                    $data['lossVal'] = $info['lossVal'];
                    $str = serialize($data);
                    break;
                case 7:
                    $data['moenyOdds'] = $info['moenyOdds'];
                    $data['moenyVal'] = $info['moenyVal'];
                    $str = serialize($data);
                    break;
                case 8:
                    $data['expOdds'] = $info['expOdds'];
                    $data['expVal'] = $info['expVal'];
                    $str = serialize($data);
                    break;
                case 9:
                    $data['jettonVal'] = $info['jettonVal'];
                    $str = serialize($data);
                    break;
                case 10:
                    $data['moneyVal'] = $info['moneyVal'];
                    $str = serialize($data);
                    break;
                default :break;
            }
            return $str;
        } 
}